package com.hellobike.haxing.modules.search.controller;

import java.util.*;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hellobike.haxing.common.util.RedisUtil;
import com.hellobike.haxing.common.vo.Result;
import com.hellobike.haxing.common.system.query.QueryGenerator;
import com.hellobike.haxing.common.util.oConvertUtils;
import com.hellobike.haxing.modules.search.entity.SearchHotWord;
import com.hellobike.haxing.modules.search.service.ISearchHotWordService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import com.hellobike.haxing.common.system.base.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import com.hellobike.haxing.common.annotation.AutoLog;

/**
 * @Description: t_search_hot_word
 * @Author: haxing-boot
 * @Date: 2020-02-19
 * @Version: V1.0
 */
@RestController
@RequestMapping("/search/searchHotWord")
@Slf4j
public class SearchHotWordController extends BaseController<SearchHotWord, ISearchHotWordService> {
    @Autowired
    private ISearchHotWordService searchHotWordService;

    @Autowired
    private RedisUtil redisUtil;


    @RequestMapping(value = "/custom", method = {RequestMethod.GET})
    public String custom(HttpServletRequest request, HttpServletResponse response) {
        String eTag = request.getHeader("If-None-Match");
        Long modified= request.getDateHeader("If-Modified-Since");
        Set<Object> set = redisUtil.sGet("hx:custom:token");
        //设置头
        if(null == modified || -1 == modified) {
            //如果没有，则使用当前时间
            modified = System.currentTimeMillis();
        }
        String oldEtag = set.size() + "";
        response.setDateHeader("Last-Modified", Long.valueOf(modified));
        response.setHeader("ETags", set.size() + "");
        StringBuilder sb = new StringBuilder("");
        if(!oldEtag.equals(eTag)) {
            if (!CollectionUtils.isEmpty(set)) {
                set.forEach(obj -> {
                    sb.append(obj).append("\r\n");
                });
            }
            response.setDateHeader("Last-Modified", System.currentTimeMillis());
        }
        return sb.toString();
    }

    /**
     * 分页列表查询
     *
     * @param searchHotWord
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "t_search_hot_word-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(SearchHotWord searchHotWord,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<SearchHotWord> queryWrapper = QueryGenerator.initQueryWrapper(searchHotWord, req.getParameterMap());
        Page<SearchHotWord> page = new Page<SearchHotWord>(pageNo, pageSize);
        IPage<SearchHotWord> pageList = searchHotWordService.page(page, queryWrapper);
        return Result.ok(pageList);
    }

    /**
     * 添加
     *
     * @param searchHotWord
     * @return
     */
    @AutoLog(value = "t_search_hot_word-添加", operateType = 2)
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody SearchHotWord searchHotWord) {
        searchHotWordService.save(searchHotWord);
        return Result.ok("添加成功！");
    }

    /**
     * 编辑
     *
     * @param searchHotWord
     * @return
     */
    @AutoLog(value = "t_search_hot_word-编辑", operateType = 3)
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody SearchHotWord searchHotWord) {
        searchHotWordService.updateById(searchHotWord);
        return Result.ok("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "t_search_hot_word-删除", operateType = 4)
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) Long id) {
        searchHotWordService.removeById(id);
        return Result.ok("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "t_search_hot_word-批量删除", operateType = 4)
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.searchHotWordService.removeByIds(Arrays.asList(ids.split(",")).stream().map(s -> Long.parseLong(s.trim())).collect(Collectors.toList()));
        return Result.ok("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) Long id) {
        SearchHotWord searchHotWord = searchHotWordService.getById(id);
        if (searchHotWord == null) {
            return Result.error("未找到对应数据");
        }
        return Result.ok(searchHotWord);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param searchHotWord
     */
    @AutoLog(value = "t_search_hot_word-导出excel", operateType = 6)
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, SearchHotWord searchHotWord) {
        return super.exportXls(request, searchHotWord, SearchHotWord.class, "t_search_hot_word");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @AutoLog(value = "t_search_hot_word-导入excel", operateType = 5)
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, SearchHotWord.class);
    }

}
